﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SiteLib.Data;
using SiteLib.Tools;

namespace SiteLib.AdminPanel
{
    public partial class MainMenu : System.Web.UI.Page
    {
        private const string DefaultPageUrl = "/AdminPanel/MainMenu.aspx";

        private SiteMenuItem CurrentSiteMenuItem
        {
            get { return (SiteMenuItem)ViewState["CurrentSiteMenuItem"]; }
            set { ViewState["CurrentSiteMenuItem"] = value; }
        }

        protected bool IsNewItem
        {
            get { return Convert.ToBoolean(ViewState["IsNewItem"]); }
            set { ViewState["IsNewItem"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                mvMenu.ActiveViewIndex = 0;

                List<SiteMenuItem> smiList = SiteMenuItem.GetMenuItemsTree(false);

                rptMainMenu.DataSource = smiList;
                rptMainMenu.DataBind();
            }
        }

        private void FillParentList()
        {
            List<SiteMenuItem> smiList = SiteMenuItem.GetActiveMenuItems();
            if (smiList != null)
            {
                smiList.Insert(0, new SiteMenuItem()
                {
                    Title = "[-Главное меню-]",
                    SiteMenuItemId = null
                });

                ddlParentMenuItem.DataSource = smiList;
                ddlParentMenuItem.DataBind();
            }
        }

        protected void rptMainMenu_OnItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "EditMenuItem" && e.CommandArgument != null)
            {
                mvMenu.ActiveViewIndex = 1;
                pnlSaveChanges.Visible = true;
                pnlSaveNew.Visible = false;

                SiteMenuItem smi = SiteMenuItem.Get(Convert.ToInt32(e.CommandArgument));
                if (smi != null)
                {
                    CurrentSiteMenuItem = smi;

                    FillParentList();

                    txtMenuItemTitle.Text = CurrentSiteMenuItem.Title;
                    if (CurrentSiteMenuItem.ParentSiteMenuItemId != null)
                    {
                        ddlParentMenuItem.SelectedValue = CurrentSiteMenuItem.ParentSiteMenuItemId.Value.ToString();
                    }

                    rbPageTypeInfo.Checked = (CurrentSiteMenuItem.PageType == (int)EnumHelper.PageType.InformationBlock);
                    rbPageTypeList.Checked = (CurrentSiteMenuItem.PageType == (int)EnumHelper.PageType.List);
                }
            }
            else if (e.CommandName == "DeleteMenuItem" && e.CommandArgument != null)
            {
                SiteMenuItem smi = SiteMenuItem.Get(Convert.ToInt32(e.CommandArgument));

                if (smi.KeyId != null)
                {
                    if (smi.PageType == (int)EnumHelper.PageType.InformationBlock)
                    {
                        SiteContent sc = SiteContent.Get(smi.KeyId);
                        sc.MarkAsDeleted();
                    }

                    else if (smi.PageType == (int)EnumHelper.PageType.List)
                    {
                        SiteContentType sct = SiteContentType.Get(smi.KeyId);
                        sct.MarkAsDeleted();
                    }
                }

                smi.MarkAsDeleted();
                Response.Redirect(DefaultPageUrl);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(DefaultPageUrl);
        }

        protected void btnSaveChangesMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentSiteMenuItem != null)
            {
                EnumHelper.PageType targetType = rbPageTypeInfo.Checked
                    ? EnumHelper.PageType.InformationBlock
                    : EnumHelper.PageType.List;

                string sTitle = txtMenuItemTitle.Text.Trim();

                CurrentSiteMenuItem.Title = sTitle;
                CurrentSiteMenuItem.ParentSiteMenuItemId = !String.IsNullOrEmpty(ddlParentMenuItem.SelectedValue)
                    ? Convert.ToInt32(ddlParentMenuItem.SelectedValue)
                    : (int?)null;

                CurrentSiteMenuItem.PageType = (int)targetType;

                if (targetType == EnumHelper.PageType.InformationBlock)
                {
                    SiteContent sc = SiteContent.Get(CurrentSiteMenuItem.KeyId);
                    if (sc == null)
                        return;

                    sc.Title = sTitle;
                    sc.UpdateInstance();
                }
                else if (targetType == EnumHelper.PageType.List)
                {
                    SiteContentType sct = SiteContentType.Get(CurrentSiteMenuItem.KeyId);
                    if (sct == null)
                        return;

                    sct.Title = sTitle;
                    sct.Update();
                }

                CurrentSiteMenuItem.UpdateInstance();
                Response.Redirect(DefaultPageUrl);
            }
        }

        protected void btnSaveNewMenuItem_Click(object sender, EventArgs e)
        {
            int keyId = 0;
            string sTitle = txtMenuItemTitle.Text.Trim();
            EnumHelper.PageType targetType = rbPageTypeInfo.Checked
                ? EnumHelper.PageType.InformationBlock
                : EnumHelper.PageType.List;

            if (targetType == EnumHelper.PageType.InformationBlock)
            {
                SiteContent sc = new SiteContent
                {
                    Title = sTitle,
                    CreateDate = DateTime.Today,
                    ContentTypeCode = (int)EnumHelper.ContentType.Default
                };

                sc.Save();
                if (sc.SiteContentId != null) keyId = sc.SiteContentId.Value;
            }
            else if (targetType == EnumHelper.PageType.List)
            {
                SiteContentType sct = new SiteContentType
                {
                    Title = sTitle
                };

                sct.Save();
                if (sct.SiteContentTypeId != null) keyId = sct.SiteContentTypeId.Value;
            }

            if (keyId != 0)
            {
                SiteMenuItem smi = new SiteMenuItem
                {
                    Title = sTitle,
                    ParentSiteMenuItemId =
                        !String.IsNullOrEmpty(ddlParentMenuItem.SelectedValue)
                            ? Convert.ToInt32(ddlParentMenuItem.SelectedValue)
                            : (int?)null,
                    PageType = (int)targetType,
                    KeyId = keyId,
                    Location = 999
                };

                smi.SaveInstance();
                Response.Redirect(DefaultPageUrl);
            }
        }

        protected void btnAddNewMenuItem_Click(object sender, EventArgs e)
        {
            mvMenu.ActiveViewIndex = 1;

            FillParentList();

            pnlSaveChanges.Visible = false;
            pnlSaveNew.Visible = true;
        }
    }
}